package cn.gailvlun.gll.presentation.wish.drawer;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.jakewharton.rxbinding2.InitialValueObservable;
import com.jakewharton.rxbinding2.widget.RxRadioGroup;
import com.jakewharton.rxbinding2.widget.RxTextView;

import cn.gailvlun.gll.Constants;
import cn.gailvlun.gll.R;
import cn.gailvlun.gll.databinding.ActivityCommentBinding;
import cn.gailvlun.gll.net.HttpMethods;
import cn.gailvlun.gll.net.HttpSubscriber;
import cn.gailvlun.gll.net.wish.OperationRecordReq;
import cn.gailvlun.gll.net.wish.Wish;
import cn.gailvlun.gll.presentation.base.BaseActivity;
import cn.gailvlun.gll.presentation.wish.WishType;
import cn.gailvlun.gll.util.DialogUtil;
import cn.gailvlun.gll.util.IsEmpty;
import cn.gailvlun.gll.util.RxUtil;
import cn.gailvlun.gll.util.ToastUtil;
import cn.gailvlun.gll.widget.NavigationBarView;
import io.reactivex.Observable;
import io.reactivex.functions.BiFunction;
import io.reactivex.functions.Consumer;
import rx_activity_result2.Result;
import rx_activity_result2.RxActivityResult;

public class CommentActivity extends BaseActivity {

    private int mWishType;
    private int mWishId;
    private ActivityCommentBinding mBinding;
    private OperationRecordReq req;

    public static void openActivity(Activity activity, @WishType int wishType, int wishId, Consumer<Result<Activity>> resultConsumer) {
        RxActivityResult.on(activity)
                .startIntent(new Intent(activity, CommentActivity.class)
                        .putExtra(Constants.EXTRA_WISH_TYPE, wishType)
                        .putExtra(Constants.EXTRA_WISH_ID, wishId))
                .subscribe(resultConsumer);
    }

    @Override
    protected void initComponent() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_comment);
        mBinding.setPresenter(new Presenter());
        mWishType = getIntent().getIntExtra(Constants.EXTRA_WISH_TYPE, 0);
        mWishId = getIntent().getIntExtra(Constants.EXTRA_WISH_ID, -1);
        mBinding.setWishType(mWishType);
    }

    @Override
    protected void createHandler() {
        mBinding.navigationBar.setOnBackListener(new NavigationBarView.OnBackListener() {
            @Override
            public void onBack(NavigationBarView nb) {
                onBackPressed();
            }
        });
        mBinding.navigationBar.setOnMenuClickListener(new NavigationBarView.OnMenuClickListener() {
            @Override
            public void onMenuClick(NavigationBarView nb) {
                req.setText(mBinding.cevContent.getText());
                int checkedId = mBinding.rgEmotionType.getCheckedRadioButtonId();
                if (checkedId == R.id.rb_satisfactory) {
                    req.setScore(Constants.SATISFACTORY);

                } else if (checkedId == R.id.rb_normal) {
                    req.setScore(Constants.NORMAL);

                } else if (checkedId == R.id.rb_boring) {
                    req.setScore(Constants.BORING);
                } else if (checkedId == R.id.rb_perfunctory) {
                    req.setScore(Constants.PERFUNCTORY);

                }
                comment(req);
            }

        });
        InitialValueObservable<Integer> rgObservable = RxRadioGroup.checkedChanges(mBinding.rgEmotionType);
        InitialValueObservable<CharSequence> cevObservable = RxTextView.textChanges(mBinding.cevContent.getEditText());
        Observable.combineLatest(rgObservable, cevObservable, new BiFunction<Integer, CharSequence, Boolean>() {
            @Override
            public Boolean apply(Integer checkedId, CharSequence charSequence) throws Exception {













                boolean contentValid = charSequence.length() > 0;
                return  contentValid;
            }
        })
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        mBinding.navigationBar.setMenuEnable(aBoolean);
                        mBinding.navigationBar.setMenuTextColor(aBoolean ? R.color.apricot : R.color.pinkishGrey);
                    }
                });
    }

    private void comment(OperationRecordReq req) {
        if (req == null || IsEmpty.string(req.getText().trim())) {
            ToastUtil.showShort("内容不能为空哦～");
            return;
        }

        final Dialog dialog = DialogUtil.showIndeterminate(mContext);
        HttpMethods.getWishService()
                .comment(req)
                .compose(RxUtil.<Wish>applyScheduler())
                .compose(this.<Wish>bindToLifecycle())
                .subscribe(new HttpSubscriber<Wish>() {
                    @Override
                    protected void onSuccess(Wish response) {
                        dialog.dismiss();
                        setResult(RESULT_OK);
                        finish();
                    }

                    @Override
                    protected void onFailure(String errMsg, @Nullable Wish response, int code) {
                        dialog.dismiss();
                        DialogUtil.showError(mContext, errMsg);
                    }
                });
    }

    @Override
    protected void loadData(@Nullable Bundle savedInstanceState) {
        req = new OperationRecordReq()
                .setWish_id(mWishId)
                .setAction("score");
        mBinding.rgEmotionType.check(R.id.rb_satisfactory);
    }

    public class Presenter {

        public String getValidity(int wishType) {
            return getString(R.string.wish_drawer_comment_hint, wishType == 1 ? "救世主" : "许愿人");
        }

    }

}
